/*
 * @Author: PanRS
 * @CreateDate：2015年9月29日
 *   将html生成canvas标签并以图片格式保存到本地中(注：1、考虑到跨域问题；2、图片后缀问题，在修改中..)
 *   基于html2canvas.min.js / canvas2image.js
 * 
 *   @update by PanRS
 */

var Html2Canvas;
if (!window.Html2Canvas) {
	Html2Canvas = {};
} else {
	//throw new Error("Html2Canvas has already existed!");
}

/** 
 * 将html区域转成<canvas>标签内容
 * @param divId 需要转换的html标签的id
 */
Html2Canvas.Save =  function(divId , backcall, name){
	var width = divId.width() , height = divId.height();
	var canvas = document.createElement('canvas');
	var scale = 2;
	canvas.width = width * scale;
	canvas.height = height * scale;
	var ctx = canvas.getContext('2d');
	ctx.scale(scale,scale);
	html2canvas(divId, {
		useCORS : true,
		allowTaint : false,
        logging : true,
        canvas: canvas,
		onrendered: function(canvas) {
			Canvas2Image.saveAsImage(canvas, canvas.width, canvas.height, "png", name);
			if(backcall != undefined && typeof backcall == 'function'){
				backcall(true);
			}
		}
	});
};

/** 
 * 将html区域转成<canvas>标签内容
 * @param divId 需要转换的html标签的id
 */
Html2Canvas.Print =  function(divId , backcall){
	html2canvas(divId, {
		allowTaint : false,
		useCORS : true,
		//logging : true,
		onrendered: function(canvas) {
			var div = document.getElementById("cvs-wrap");
			div = $(div).length > 0 ? div : document.createElement('div');
			$(div).attr('id' , 'cvs-wrap').css({'display' : 'none'});
			
			document.body.appendChild(div);
			$(div).html("").append($(canvas));
			
			var cvsId = "id-" + (new Date().getTime()); 
			$(canvas).attr("id" , cvsId);
			var canvas = document.getElementById(cvsId);
			var imgdata = Canvas2Image.getPrintImageData(canvas, divId.width(), divId.height(), "png");
			if(backcall != undefined && typeof backcall == 'function'){
				backcall(true , imgdata);
			}
		}
	});
};
